export function fillSpace(str, long, mode = 'right') {
  let count = long - str.length;
  if (count < 0) {
    return str;
  }
  if (mode === 'right') {
    return str + " ".repeat(count)
  } else if (mode === 'left') {
    return " ".repeat(count) + str
  }
}

export function fixPercent(str) {
  let value = Math.round(parseFloat(str) * 100) / 100;
  let xsd = value.toString().split(".");
  if (xsd.length == 1) {
    value = value.toString() + ".00";
    return value;
  }
  if (xsd.length > 1) {
    if (xsd[1].length < 2) {
      value = value.toString() + "0";
    }
    return value;
  }
}


export function timeTransform(time) {
  if(!time){
    return
  }
  let time_arr = [];
  let range = time.split("~");
  let time_range = range.map(item => {
    return {
      year: +item.split("-")[0],
      month: +item.split("-")[1]
    };
  });
  let from = time_range[0];
  let to = time_range[1];
  while (
    from.year < to.year ||
    (from.year === to.year && from.month <= to.month)
  ) {
    if (from.month < 12) {
      from.month++;
    } else {
      from.month = 1;
      from.year++;
    }
    let from_month =
      from.month > 10 ? from.month.toString() : "0" + from.month;
    time_arr.push(from.year.toString() + "-" + from_month);
  }
  return time_arr;
}


export function priceFilter(num) {
  return num && num.toString()
    .replace(/^\d+/g, (m) => m.replace(/(?=(?!^)(\d{3})+$)/g, ','))
}
